﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OMXParamsConfig
{
    /// <summary>
    /// Generates a view of the possible OMX Config program properties
    /// </summary>
    public partial class ProgProp : Form
    {
        /// <summary>
        /// Public constructor
        /// </summary>
        public ProgProp()
        {
            InitializeComponent();
            tblLayPanel.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
        }

        private void ProgProp_Load(object sender, EventArgs e)
        {
            CommLink.Properties.Settings.Default.Reload();
            IEnumerator iEnum = CommLink.Properties.Settings.Default.Properties.GetEnumerator();
            Graphics gfx = CreateGraphics();
            SizeF tmpStringMeasure;
            while (iEnum.MoveNext())
            {
                Label tmpLbl = new Label();
                tmpLbl.Text = ((System.Configuration.SettingsProperty)iEnum.Current).Name;
                tmpStringMeasure = gfx.MeasureString(tmpLbl.Text, tmpLbl.Font);
                tmpLbl.Size = new Size((int)tmpStringMeasure.Width + 10, tmpLbl.Size.Height);
                tblLayPanel.Controls.Add(tmpLbl);
                
                TextBox tmpTxtBx = new TextBox();
                if (CommLink.Properties.Settings.Default[tmpLbl.Text].GetType() == typeof(string))
                    tmpTxtBx.Text = (string)CommLink.Properties.Settings.Default[tmpLbl.Text];
                else if (CommLink.Properties.Settings.Default[tmpLbl.Text].GetType() == typeof(int))
                    tmpTxtBx.Text = ((int)CommLink.Properties.Settings.Default[tmpLbl.Text]).ToString();
                tblLayPanel.Controls.Add(tmpTxtBx);
            }
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < tblLayPanel.Controls.Count; i += 2)
            {
                Label tmpLbl = (Label)tblLayPanel.Controls[i];
                TextBox tmpTxtBx = (TextBox)tblLayPanel.Controls[i + 1];

                if (CommLink.Properties.Settings.Default[tmpLbl.Text].GetType() == typeof(string))
                    CommLink.Properties.Settings.Default[tmpLbl.Text] = tmpTxtBx.Text;
                else if (CommLink.Properties.Settings.Default[tmpLbl.Text].GetType() == typeof(int))
                    CommLink.Properties.Settings.Default[tmpLbl.Text] = int.Parse(tmpTxtBx.Text);
            }

            CommLink.Properties.Settings.Default.Save();
        }
    }
}
